# 银行账号自动存取款系统
import getpass

# 定义3个账号信息
account_bank_Infomation_Dict = {
    'a': {'name': '路飞', 'age': '26', 'sex': '男', 'ID_Card': '123', 'password': '111', 'moneys': '50000'},
    'b': {'name': '山治', 'age': '27', 'sex': '男', 'ID_Card': '456', 'password': '444', 'moneys': '40000'},
    'c': {'name': '索隆', 'age': '28', 'sex': '男', 'ID_Card': '789', 'password': '777', 'moneys': '30000'},
}


def CheacInAccouts(bank_Accounts, ID_Card):
    for account_key in bank_Accounts.keys():
        accounts = bank_Accounts[account_key]
        if ID_Card == accounts['ID_Card']:
            return True, account_key, accounts
    return False, '当前用户不存在', {'msg': '当前用户不存在'}


# 编写一个展示操作函数
def account_Handle(name):
    print('尊敬的 ' + name + ' 先生您好,请选择您需要操作的功能！')
    print('{:1}{:8}{:1}{:8}'.format(' ', '1.余额', ' ', '2.取钱'))
    print('{:1}{:8}{:1}{:8}'.format(' ', '3.转账', ' ', '4.存钱'))
    print('{:1}{:8}'.format(' ', '5.退出'))
    sid = input('请输入对应的选择编号：')
    return int(sid)


# 编写一个判断大小函数
def compareMoneys(money1, money2):
    if money1 >= money2:
        return True, str(money1 - money2)
    else:
        return False, '当前余额不足'


# 编写一个取款函数
def withDrawals(bank_Accounts):
    accounts_Input_Moneys = input('请输入您要取款的金额：')
    if accounts_Input_Moneys == '':
        print('输入金额有误！')
        return False, '输入金额有误！'
    else:
        accounts_Curren_Moneys = bank_Accounts['moneys']
        (money_Bool, moneys) = compareMoneys(int(accounts_Curren_Moneys), int(accounts_Input_Moneys))
        if money_Bool:
            print('取款成功,当前的账户余额为：' + moneys)
            bank_Accounts['moneys'] = moneys
            return True, bank_Accounts
        else:
            print('当前余额不足,请重新输入')
            return False, '当前余额不足,请重新输入'


accounts = {}
print('=' * 20 + '  自动存取款系统  ' + '=' * 20)
ID_Card = ''
password = ''
while True:
    if ID_Card == '':
        ID_Card = input('请输入您的身份证号：')
    if password == '':
        password = getpass.getpass('请输入您的密码：')

    # 检查当前用户是否存在，存在则返回用户信息
    (in_Account_Bool, account_ID, accounts) = CheacInAccouts(account_bank_Infomation_Dict, ID_Card)

    if in_Account_Bool:
        if accounts['password'] == password:
            break
        else:
            password = ''
            print('您输入的用户密码不正确,请重新输入')
    else:
        ID_Card = ''
        password = ''
        print('您输入的用户不存在,请重新输入')

# 获取用户操作指令
handle_Sid = account_Handle(accounts['name'])
while True:
    if handle_Sid == 1:
        # 查询
        print('您当前的账户余额为：' + accounts['moneys'])
        print('-' * 50)
        handle_Sid = account_Handle(accounts['name'])
    elif handle_Sid == 2:
        # 取钱
        (drawals_Bool, single_Accounts) = withDrawals(accounts)
        if drawals_Bool:
            account_bank_Infomation_Dict[account_ID] = single_Accounts
        else:
            withDrawals(accounts)
        print('-' * 50)
        handle_Sid = account_Handle(accounts['name'])
    elif handle_Sid == 3:
        # 转账
        accept_account = input('请输入您要转账的账户：')

        if accept_account == accounts['ID_Card']:
            print('不能向自己的账户转账！')
            handle_Sid = account_Handle(accounts['name'])
            break

        # 检查当前用户是否存在，存在则返回用户信息
        (in_accept_Account_Bool, accept_ID, accept_accounts) = CheacInAccouts(account_bank_Infomation_Dict,
                                                                              accept_account)
        if in_accept_Account_Bool:
            send_Moneys = input('请输入您要转账的金额：')

            if send_Moneys == '':
                print('输入金额有误！')
            else:
                current_Moneys = int(accounts['moneys'])
                send_Moneys = int(send_Moneys)
                if current_Moneys >= send_Moneys:
                    # 更新用户信息
                    accounts['moneys'] = str(current_Moneys - send_Moneys)
                    account_bank_Infomation_Dict[account_ID] = accounts
                    # 更新接收方用户信息
                    accept_accounts['moneys'] = str(int(accept_accounts['moneys']) + send_Moneys)
                    account_bank_Infomation_Dict[accept_ID] = accept_accounts
                    print('转账成功，当前账户余额为：' + accounts['moneys'])
                else:
                    print('您当前余额不足')

        else:
            print('您输入的用户不存在')
        print('-' * 50)
        handle_Sid = account_Handle(accounts['name'])
    elif handle_Sid == 4:
        # 存钱
        save_Money = input('请输入您要存储的金额：')
        if save_Money == '':
            print('输入金额有误！')
        else:
            accounts['moneys'] = str(int(accounts['moneys']) + int(save_Money))
            account_bank_Infomation_Dict[account_ID] = accounts
            print('存储成功，当前账户余额为：' + accounts['moneys'])
        print('-' * 50)
        handle_Sid = account_Handle(accounts['name'])
    elif handle_Sid == 5:
        print('注销成功')
        break
    else:
        print('输入功能不存在，请重新输入！')
        print('-' * 50)
        handle_Sid = account_Handle(accounts['name'])
